
package kaizen.nomweb;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.Properties;
import java.util.Vector;

import javax.servlet.*;
import javax.servlet.http.*;

import kaizen.jdbc.DbAppUnivC;
import kaizen.jdbc.dbRecord;

// Referenced classes of package kaizen.nomweb:
//            ejecuta, templates, utilerias

public class cancelacion extends HttpServlet
{

  public cancelacion()
  {
  }

  public static String cancela(HttpServletRequest httpservletrequest, 
                               HttpServletResponse httpservletresponse, 
                               String s) throws IOException
  {
    HttpSession httpsession = httpservletrequest.getSession(false);
    String s1 = "";
    String s3 = "";
    String s6 = "";
    String s7 = "";
    generaAmbiente(httpservletrequest);
    String s9 = 
      utilerias.validaNulo(httpservletrequest.getParameter("edo_pago"));
    if (s9.equals("") || s9.equals("null"))
    {
      String s4 = templates.generaHidden("edo_pago", "0");
      String s2 = templates.generaTexto("txtfolio", "13", "13", "");
      String s10 = 
        "<script language='JavaScript'> \n <!-- \n  function validaFolio () { \n    var folio = document.data.txtfolio.value; \n    if (folio.indexOf('-') == -1) { \n       alert('Formato de Folio Incorrecto: \\n Formato valido: serie-numero'); \n       return false; \n    } \n   else \n    createCookie('cancelapedido','" + 
        s + "', 1); \n" + "} \n " + 
        "function createCookie(name,value,days)\n" + "{\n" + "   if (days)\n" + 
        "   {\n" + "      var date = new Date();\n" + 
        "      date.setTime(date.getTime()+(days*24*60*60*1000));\n" + 
        "      var expires = \"; expires=\"+date.toGMTString();\n" + "   }\n" + 
        "   else var expires = \"\";\n" + 
        "   document.cookie = name+\"=\"+value+expires+\";\";\n" + "}\n" + 
        "\n" + "\n //--> \n </script> \n";
      String s13 = templates.getHost();
      String s16 = dirPlantillasGenerales + plant_cancela;
      s6 = templates.generaPlantilla(s16, s13);
      s6 = utilerias.replaceString1(s6, "--script--", s10);
      s6 = utilerias.replaceString1(s6, "--folio--", s2);
      s6 = utilerias.replaceString1(s6, "--error--", "");
      s6 = utilerias.replaceString1(s6, "--hidden--", s4);
    } else
    {
      if (s9.equals("0"))
      {
        String s5 = templates.generaHidden("edo_pago", "1");
        String s8 = 
          utilerias.validaNulo(httpservletrequest.getParameter("txtfolio"));
        s5 = s5 + templates.generaHidden("folio", s8);
        String s11 = 
          "SELECT COUNT(*)  FROM SCC_PEDIDOS  WHERE pedi_foin_serie  is not null  AND   pedi_foin_numero is not null  AND   pedi_foin_serie  = UPPER(substr('" + 
          s8 + "',1,instr('" + s8 + "','-')-1)) " + 
          " AND   pedi_foin_numero = UPPER(substr('" + s8 + "',instr('" + s8 + 
          "','-')+1)) ";
        String s14 = utilerias.valorSqlUni(s11);
        if (s14.equals("0"))
        {
          String s17 = templates.getHost();
          String s19 = dirPlantillasGenerales + plant_cancela_datos;
          s6 = templates.generaPlantilla(s19, s17);
          s6 = utilerias.replaceString1(s6, "--script--", "");
          s6 = utilerias.replaceString1(s6, "--tabla--", "");
          s6 = 
              utilerias.replaceString1(s6, "--error--", "No Existe el Folio de Internet.");
          s6 = utilerias.replaceString1(s6, "--hidden--", "");
        } else
        {
          String s18 = 
            " select pedi_serie||'-'||pedi_numero,  pedi_prod_clave||'-'||prod_descripcion,  pedi_toneladas,  pedi_referencia,  DECODE(pedi_status,'A','Activo','X','Cancelado','C','Cerrado','S','Suspendido'),  pedi_foin_serie||'-'||pedi_foin_numero  from   scc_pedidos, scc_productos  where  prod_clave       = pedi_prod_clave  and    pedi_foin_serie  is not null  and    pedi_foin_numero is not null  and    pedi_foin_serie  = UPPER(substr('" + 
            s8 + "',1,instr('" + s8 + "','-')-1)) " + 
            " and    pedi_foin_numero = UPPER(substr('" + s8 + "',instr('" + 
            s8 + "','-')+1)) ";
          Vector vector = DbAppUnivC.execQuery(s18);
          String s22 = "";
          String s25 = "";
          String s29 = "";
          String s33 = "";
          String s37 = "";
          String s41 = "";
          String s46 = "";
          String s49 = "";
          String s53 = "";
          int j = vector.size();
          s5 = s5 + templates.generaHidden("tam", String.valueOf(j));
          for (int k = 0; k < vector.size(); k++)
          {
            dbRecord dbrecord = (dbRecord)vector.elementAt(k);
            String s26 = dbrecord.getField(1);
            String s30 = dbrecord.getField(2);
            String s34 = dbrecord.getField(3);
            String s38 = dbrecord.getField(4);
            String s42 = dbrecord.getField(5);
            String s50;
            String s54;
            if (s42.equals("Cancelado"))
            {
              s50 = 
                  "<INPUT TYPE='checkbox' NAME='chk_" + k + "' value='X'   disabled OnClick='document.data.chk_" + 
                  k + 1 + ".checked=false;'>";
              s54 = 
                  "<INPUT TYPE='checkbox' NAME='chk_" + k + 1 + "' value='C' disabled OnClick='document.data.chk_" + 
                  k + ".checked=false;'>";
            } else if (s42.equals("Cerrado"))
            {
              s50 = 
                  "<INPUT TYPE='checkbox' NAME='chk_" + k + "' value='X'   OnClick='document.data.chk_" + 
                  k + 1 + ".checked=false;'>";
              s54 = 
                  "<INPUT TYPE='checkbox' NAME='chk_" + k + 1 + "' value='C' disabled OnClick='document.data.chk_" + 
                  k + ".checked=false;'>";
            } else
            {
              s50 = 
                  "<INPUT TYPE='checkbox' NAME='chk_" + k + "' value='X'   OnClick='document.data.chk_" + 
                  k + 1 + ".checked=false;'>";
              s54 = 
                  "<INPUT TYPE='checkbox' NAME='chk_" + k + 1 + "' value='C' OnClick='document.data.chk_" + 
                  k + ".checked=false;'>";
            }
            s5 = s5 + templates.generaHidden("pedido_" + k, s26);
            s22 = s22 + generaColumna(s26, s30, s34, s38, s42, s50, s54);
          }

          String s57 = templates.getHost();
          String s59 = dirPlantillasGenerales + plant_cancela_datos;
          String s61 = 
            "<script language='JavaScript'> \n <!-- \n function createCookie(name,value,days)\n{\n   if (days)\n   {\n      var date = new Date();\n      date.setTime(date.getTime()+(days*24*60*60*1000));\n      var expires = \"; expires=\"+date.toGMTString();\n   }\n   else var expires = \"\";\n   document.cookie = name+\"=\"+value+expires+\";\";\n}\n\nfunction getCookieVal (offset)\n{\n   var endstr = document.cookie.indexOf (\";\", offset);\n   if (endstr == -1)\n      endstr = document.cookie.length;\n   return unescape(document.cookie.substring(offset, endstr));\n}\n\nfunction GetCookie (name)\n{\n   var arg = name + \"=\";\n   var alen = arg.length;\n   var clen = document.cookie.length;\n   var i = 0;\n   while (i < clen)\n   {\n      var j = i + alen;\n      if (document.cookie.substring(i, j) == arg)\n         return getCookieVal (j);\n      i = document.cookie.indexOf(\" \", i) + 1;\n      if (i == 0) break;\n   }\n   return null;\n}\nfunction validaCampos() { \n   if(GetCookie(\"cancelapedido\") == null)\n  {\n    document.write(\"<table width=\\\"100%\\\" height=\\\"100%\\\" border=\\\"0\\\" background=\\\"/scm/media/fondop.jpg\\\">\");\n    document.write(\"  <tr>\");\n    document.write(\"    <td><div align=\\\"center\\\">Su transacci\363n a expirado.<br><a href=\\\"/servlet/servlet/ConectAc?dic_estado=92&dic_menu=m01\\\">Para regresar a <img src=\\\"/scm/media/integra/interior_01.jpg\\\" alt=\\\"enlace@montezuma\\\" width=\\\"187\\\" height=\\\"41\\\" align=\\\"middle\\\"> dar click en la imagen.<a><br></div></td>\");\n    document.write(\"  </tr>\");\n    document.write(\"</table>\");\n    return false;\n  }\nelse \n    createCookie(\"cancelapedido\",\"\",-1);\n } \n \n //--> \n </script> \n";
          s6 = templates.generaPlantilla(s59, s57);
          s6 = utilerias.replaceString1(s6, "--script--", s61);
          s6 = utilerias.replaceString1(s6, "--tabla--", s22);
          s6 = utilerias.replaceString1(s6, "--error--", "");
          s6 = utilerias.replaceString1(s6, "--hidden--", s5);
        }
      }
      if (s9.equals("1"))
      {
        String s12 = "";
        String s15 = 
          utilerias.validaNulo(httpservletrequest.getParameter("tam"));
        int i = Integer.parseInt(s15);
        String s20 = "";
        String s23 = "";
        String s27 = "";
        String s31 = "";
        String s35 = "";
        String s39 = "";
        String s43 = "";
        String s47 = "";
        String s51 = "";
        String s55 = "";
        PrintWriter printwriter = httpservletresponse.getWriter();
        for (int l = 0; l < i; l++)
        {
          String s56 = "0";
          String s21 = 
            utilerias.validaNulo(httpservletrequest.getParameter("chk_" + l));
          String s24 = 
            utilerias.validaNulo(httpservletrequest.getParameter("chk_" + l + 
                                                                 1));
          String s28 = 
            utilerias.validaNulo(httpservletrequest.getParameter("pedido_" + 
                                                                 l));
          String s32 = s28.substring(0, s28.indexOf("-"));
          String s36 = s28.substring(s28.indexOf("-") + 1, s28.length());
          String s40 = s32 + "," + s36;
          if (s21.equals("X") || s24.equals("X"))
          {
            s56 = "1";
            String s44 = s40 + ",X,";
            ejecuta ejecuta1 = new ejecuta();
            String s48 = 
              ejecuta.store("SCC_P_CANCELA_PEDIDO(,V,N,V,V)", s44, 3, 1);
            if (!s48.equals("S;"))
              s12 = s12 + s28 + " " + s48 + "<br>";
            else
              s12 = 
                  s12 + "El Pedido " + s28 + " Fue Cancelado Correctamente. <br>";
          } else if (s21.equals("C") || s24.equals("C"))
          {
            s56 = "1";
            String s45 = s40 + ",C,";
            ejecuta ejecuta2 = new ejecuta();
            String s52 = 
              ejecuta.store("SCC_P_CANCELA_PEDIDO(,V,N,V,V)", s45, 3, 1);
            if (!s52.equals("S;"))
              s12 = s12 + s28 + " " + s52 + "<br>";
            else
              s12 = 
                  s12 + "El Pedido " + s28 + " Fue Cerrado Correctamente. <br>";
          }
          if (s56.equals("0"))
            s12 = s12 + "El Pedido " + s28 + " No fue Modificado. <br>";
        }

        String s58 = templates.getHost();
        String s60 = dirPlantillasGenerales + plant_cancela_confirma;
        s6 = templates.generaPlantilla(s60, s58);
        s6 = utilerias.replaceString1(s6, "--error--", s12);
      }
    }
    return s6;
  }

  public void doGet(HttpServletRequest httpservletrequest, 
                    HttpServletResponse httpservletresponse) throws ServletException, 
                                                                    IOException
  {
    httpservletresponse.setContentType("text/html");
    PrintWriter printwriter = httpservletresponse.getWriter();
    HttpSession httpsession = httpservletrequest.getSession(false);
    if (httpsession != null && httpsession.getAttribute("usuario") != null)
      usuario = httpsession.getAttribute("usuario").toString();
    if (usuario == null)
      usuario = "";
    if (usuario.length() == 0)
      printwriter.println("Error, usuario no valido");
    String s = cancela(httpservletrequest, httpservletresponse, usuario);
    printwriter.println(s);
  }

  public void doPost(HttpServletRequest httpservletrequest, 
                     HttpServletResponse httpservletresponse) throws IOException, 
                                                                     ServletException
  {
    doGet(httpservletrequest, httpservletresponse);
  }

  public static void generaAmbiente(HttpServletRequest httpservletrequest) throws IOException
  {
    String s = "mocte";
    String s1 = "/kaizen/" + s + "Web.properties";
    Properties properties = utilerias.loadProperties(s1);
    dirPlantillasGenerales = properties.getProperty("plantillas");
    datColorRen = "";
    datFont = properties.getProperty("datFont");
    datTamFont = properties.getProperty("datTamFont");
    datColorFont = properties.getProperty("datColorFont");
  }

  public static String generaColumna(String s, String s1, String s2, String s3, 
                                     String s4, String s5, String s6)
  {
    String s7 = "";
    s7 = s7 + "<tr>";
    s7 = 
        s7 + templates.tablaCol(s, datColorFont, datFont, datTamFont, datColorRen);
    s7 = 
        s7 + templates.tablaCol(s1, datColorFont, datFont, datTamFont, datColorRen);
    s7 = 
        s7 + templates.tablaCol(s2, datColorFont, datFont, datTamFont, datColorRen);
    s7 = 
        s7 + templates.tablaCol(s3, datColorFont, datFont, datTamFont, datColorRen);
    s7 = 
        s7 + templates.tablaCol(s4, datColorFont, datFont, datTamFont, datColorRen);
    s7 = 
        s7 + templates.tablaCol(s5, datColorFont, datFont, datTamFont, datColorRen);
    s7 = 
        s7 + templates.tablaCol(s6, datColorFont, datFont, datTamFont, datColorRen);
    s7 = s7 + "</tr>";
    return s7;
  }

  static String dirPlantillasGenerales = "";
  static String datColorRen = "";
  static String datFont = "";
  static String datTamFont = "";
  static String datColorFont = "";
  static String usuario = "";
  static String plant_cancela = "pgral_ideograma/cancela.html";
  static String plant_cancela_datos = "pgral_ideograma/cancela_datos.html";
  static String plant_cancela_confirma = 
    "pgral_ideograma/cancela_confirma.html";

}
